﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TLTuan.App.TransferObj;
using System.Collections.Generic;
using System.Data.SqlClient;


namespace TLTuan.App.AccessLayer
{
    public class CDAOChungLoai
    {
        internal DataTable SelectAll2Dt()
        {
            return CConnection.GetData("pr_tblChungloai_SelectAll");
        }

        public List<CChungLoai> SelectAll2Obj()
        {
            DataTable v_dt = this.SelectAll2Dt();
            if (v_dt == null || v_dt.Rows.Count == 0)
                return null;
            else
            {
                List<CChungLoai> op_lst = new List<CChungLoai>();
                foreach (DataRow item in v_dt.Rows)
                {
                    op_lst.Add(convertDataRow2Obj(item));
                }

                return op_lst;
            }
        }

        internal bool Delete(int ip_Id)
        {
            SqlConnection v_conn = CConnection.GetConnection();

            SqlCommand v_comm = new SqlCommand("[pr_tblChungloai_Delete]", v_conn);
            v_comm.CommandType = CommandType.StoredProcedure;
            v_comm.Parameters.Add("@PK_ChungloaiID", SqlDbType.Int).Value = ip_Id;
            int v_row = v_comm.ExecuteNonQuery();

            // Close connection
            CConnection.Close();

            if (v_row > -1)
                return true;
            else
                return false;
        }

        public CChungLoai SelectOne(int ip_ID)
        {
            DataTable v_dt = CConnection.GetData("pr_tblChungloai_SelectOne", "@PK_ChungloaiID", ip_ID);
            if (v_dt == null || v_dt.Rows.Count == 0)
                return null;
            else
            {
                return convertDataRow2Obj(v_dt.Rows[0]);
            }
        }

        internal bool Update(TransferObj.CChungLoai m_category)
        {
            return (CConnection.ExecuteNonQuery("[pr_tblChungloai_Update]",
                "@PK_ChungloaiID", m_category.PK_ChungloaiID,
                 "@Machungloai", m_category.Machungloai,
                 "@Tenchungloai", m_category.Tenchungloai,
                 "@Mota", m_category.Mota) > 0);
        }

        internal bool Insert(TransferObj.CChungLoai m_category)
        {
            return (CConnection.ExecuteNonQuery("[pr_tblChungloai_Insert]",
                "@PK_ChungloaiID", m_category.PK_ChungloaiID,
                 "@Machungloai", m_category.Machungloai,
                 "@Tenchungloai", m_category.Tenchungloai,
                 "@Mota", m_category.Mota) > 0);
        }


        #region Method ...

        private CChungLoai convertDataRow2Obj(DataRow ip_dr)
        {
            // Khoi tao
            CChungLoai op_chungloai = new CChungLoai();

            // Gan cac thuoc tinh
            op_chungloai.PK_ChungloaiID = int.Parse(ip_dr["PK_ChungloaiID"].ToString());
            op_chungloai.Machungloai = ip_dr["Machungloai"].ToString();
            op_chungloai.Tenchungloai = ip_dr["Tenchungloai"].ToString();
            op_chungloai.Mota = ip_dr["Mota"].ToString();
            return op_chungloai;
        }

        #endregion
    }
}
